package sk.sigp.aobot.swinggui.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import sk.sigp.aobot.base.*;

import javax.swing.table.AbstractTableModel;

public class GroupTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 2317089906291319591L;
	private List<DoubleVal> data;
	
	public List<DoubleVal> getData() {
		if (data == null) {
			data = new ArrayList<DoubleVal>();				
		}
		return data;
	}
	
	public void addGroup(String groupName) {
		getData().add(new DoubleVal(groupName, true));
		Collections.sort(getData());
		fireTableDataChanged();
	}

	public void removeGroup(String groupName) {
		if (getData().contains(groupName))
			getData().remove(groupName);
		Collections.sort(getData());
		fireTableDataChanged();
	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 1:
			getData().get(rowIndex).setData((Boolean) value);
			break;
		}
	}
	
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object data = "?";
		switch (columnIndex) {
		case 0:
			data = getData().get(rowIndex).toString();
			break;
		case 1:
			data = getData().get(rowIndex).getData();
			break;
		}
		return data;
	}

	public int getRowCount() {
		return getData().size();
	}

	public int getColumnCount() {
		return 3;
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 1 || columnIndex == 2)
			return true;
		return false;
	}
	
	@Override
	public String getColumnName(int column) {
		String data = "?";
		switch (column) {
		case 0:
			data = "Group";
			break;
		case 1:
			data = "A";
			break;
		case 2:
			data = "O";
			break;
		}			
		return data;
	}
};
